using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace WindowsAPI
{
    public static partial class Kernel32
    {
        public static class FileMapping
        {
            [DllImport(FileName, SetLastError = true, CharSet = CharSet.Auto)]
            public static extern IntPtr CreateFileMapping(
                IntPtr hFile,
                IntPtr lpFileMappingAttributes,
                FileMapProtection flProtect,
                uint dwMaximumSizeHigh,
                uint dwMaximumSizeLow,
                [MarshalAs(UnmanagedType.LPTStr)] string lpName);

            [DllImport(FileName, SetLastError = true)]
            public static extern bool FlushViewOfFile(IntPtr lpBaseAddress,
               Int32 dwNumberOfBytesToFlush);

            [DllImport(FileName, SetLastError = true)]
            public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);

            public static IntPtr CreateFileMapping(FileStream File, FileMapProtection flProtect, long ddMaxSize, string lpName)
            {
                var Hi = (Int32)(ddMaxSize / Int32.MaxValue);
                var Lo = (Int32)(ddMaxSize % Int32.MaxValue);
                return CreateFileMapping(File.SafeFileHandle, IntPtr.Zero, flProtect, Hi, Lo, lpName);
            }

            [Obsolete("Using CreateFileMapping(SafeFileHandle hFile, ...", false)]
            [DllImport(FileName, CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr CreateFileMapping(
                IntPtr hFile,
                IntPtr lpAttributes = default(IntPtr),
                FileMapProtection flProtect = FileMapProtection.PageReadonly,
                int dwMaxSizeHi = 0,
                int dwMaxSizeLow = 0,
                string lpName = null);

            [DllImport(FileName, CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr CreateFileMapping(
                SafeFileHandle hFile,
                IntPtr lpAttributes = default(IntPtr),
                FileMapProtection flProtect = FileMapProtection.PageReadonly,
                int dwMaxSizeHi = 0,
                int dwMaxSizeLow = 0,
                string lpName = null);


            [DllImport(FileName, SetLastError = true)]
            public static extern IntPtr OpenFileMapping(FileMapAccess DesiredAccess, bool bInheritHandle, string lpName);

            [DllImport(FileName, CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr MapViewOfFile(IntPtr hFileMapping, FileMapAccess dwDesiredAccess, Int32 dwFileOffsetHigh, Int32 dwFileOffsetLow, Int32 dwNumberOfBytesToMap);

            public static IntPtr MapViewOfFile(IntPtr hFileMapping, FileMapAccess dwDesiredAccess, Int64 ddFileOffset, Int32 dwNumberOfBytesToMap)
            {
                var Hi = (Int32)(ddFileOffset / Int32.MaxValue);
                var Lo = (Int32)(ddFileOffset % Int32.MaxValue);
                return MapViewOfFile(hFileMapping, dwDesiredAccess, Hi, Lo, dwNumberOfBytesToMap);
            }
        }
    }


    internal sealed class Win32API
    {



    }
}